﻿using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ContractManager.Systems.Forms
{
    public partial class frmCompany : DevExpress.XtraEditors.XtraForm
    {
        #region "Define Variable"

        private clsCompany objCompany = new clsCompany();
        private Company _report;

        private int _companyId;
        #endregion

        #region "Construction"
        public frmCompany()
        {
            InitializeComponent();
        }
        #endregion

        #region "Private Method"

        private void getCompany()
        {
            DataTable dtCompany = objCompany.SelectAll();
            if (dtCompany != null && dtCompany.Rows.Count > 0)
            {
                _companyId = Convert.ToInt32(dtCompany.Rows[0]["ID"]);
                txtName.EditValue = String.Format("{0}", dtCompany.Rows[0]["Name"]);
                txtAddress.EditValue = String.Format("{0}", dtCompany.Rows[0]["Address"]);
                txtTel.EditValue = String.Format("{0}", dtCompany.Rows[0]["Tel"]);
                txtFax.EditValue = String.Format("{0}", dtCompany.Rows[0]["Fax"]);
                txtWebsite.EditValue = String.Format("{0}", dtCompany.Rows[0]["WebSite"]);
                txtEmail.EditValue = String.Format("{0}", dtCompany.Rows[0]["Email"]);
                txtTax.EditValue = String.Format("{0}", dtCompany.Rows[0]["Tax"]);
            }
            else
            {
                _companyId = 0;
            }
        }

        private void doSave()
        {
            #region "Check Condition"
            if (txtName.EditValue==null)
            {
                XtraMessageBox.Show(
                                "Tên đơn vị không được để trống.", 
                                "Thông báo", 
                                MessageBoxButtons.OK, 
                                MessageBoxIcon.Error);
                txtName.SelectAll();
                txtName.Focus();
                return;
            }
            #endregion

            _report = new Company();

            _report.Id = _companyId;
            _report.Name = String.Format("{0}", txtName.EditValue);
            _report.Address = String.Format("{0}", txtAddress.EditValue);
            _report.Tel = String.Format("{0}", txtTel.EditValue);
            _report.Fax = String.Format("{0}", txtFax.EditValue);
            _report.WebSite = String.Format("{0}", txtWebsite.EditValue);
            _report.Email = String.Format("{0}", txtEmail.EditValue);
            _report.Tax = String.Format("{0}", txtTax.EditValue);
            objCompany.Report = _report;
            try
            {
                if (_companyId > 0)
                {
                    objCompany.Update();
                }
                else
                {
                    _companyId = objCompany.Insert();
                }
                this.Close();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(
                    "Có lỗi xảy ra trong quá trình cập nhật thông tin đơn vị " + ex.Message, 
                    "Thông báo", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Error);
                return;
            }

        }
        #endregion

        #region "Form Event"
        private void frmCompany_Load(object sender, EventArgs e)
        {
            getCompany();
        }
        #endregion

        #region "Button Event"
        private void btnOk_Click(object sender, EventArgs e)
        {
            doSave();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        
    }
}